﻿Imports BUS

Public Class frmChamCongTay

    Private thaotac As String = "sua"

    Private Sub EnableText(ByVal b As Boolean)
        dtngay.Enabled = b
        cbomanv.Enabled = b
        txthc.Enabled = b
        txttc15.Enabled = b
        txttc195.Enabled = b
        txttc20.Enabled = b
    End Sub

    Private Sub XoaText()
        dtngay.Value = Date.Now
        cbomanv.SelectedIndex = -1
        txthc.Text = ""
        txttc15.Text = ""
        txttc195.Text = ""
        txttc20.Text = ""
    End Sub

    Private Sub GanText(ByVal index As Integer)
        dtngay.Value = dgvchamcongtay.Rows(index).Cells(2).Value
        cbomanv.SelectedValue = dgvchamcongtay.Rows(index).Cells(0).Value
        txthc.Text = dgvchamcongtay.Rows(index).Cells(3).Value
        txttc15.Text = dgvchamcongtay.Rows(index).Cells(4).Value
        txttc195.Text = dgvchamcongtay.Rows(index).Cells(5).Value
        txttc20.Text = dgvchamcongtay.Rows(index).Cells(6).Value
    End Sub

    Private Sub frmChamCongTay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sql As String = "select manv, manv+':'+tennv as tennv from NhanVien"
        cbomanv.DataSource = ProcFuncBus.ExecuteFuncSQL(sql)
        cbomanv.DisplayMember = "tennv"
        cbomanv.ValueMember = "manv"
        cbomanv.SelectedIndex = -1
        dtngay.Value = Date.Now
        EnableText(False)
    End Sub

    Private Sub dtngay_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtngay.ValueChanged
        Dim sql As String = "exec sp_laychamcongtaytheongay '" & String.Format("{0:yyyy-MM-dd}", dtngay.Value) & "'"
        dgvchamcongtay.DataSource = ProcFuncBus.ExecuteFuncSQL(sql)
        Try
            GanText(0)
        Catch ex As Exception
            Return
        End Try

    End Sub

    Private Sub ThêmToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThêmToolStripMenuItem.Click
        EnableText(True)
        thaotac = "them"
        XoaText()
    End Sub

    Private Sub SửaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SửaToolStripMenuItem.Click
        EnableText(True)
        dtngay.Enabled = False
        cbomanv.Enabled = False
        thaotac = "sua"
    End Sub

    Private Sub LưuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LưuToolStripMenuItem.Click
        Dim sql As String = ""
        If thaotac = "them" Then
            If txthc.Text = "" And txttc15.Text = "" And txttc195.Text = "" And txttc20.Text = "" Then
                MessageBox.Show("Xin vui long xem lai cham cong")
                Return
            End If
            sql = "insert into ChamCongTay(manv, ngay, hc, tc15, tc195, tc20) values (N'" & cbomanv.SelectedValue.ToString() &
                "', '" & String.Format("{0:yyyy-MM-dd}", dtngay.Value) & "', " &
                IIf(txthc.Text = "", "NULL", txthc.Text.Trim) & ", " &
                IIf(txttc15.Text = "", "NULL", txttc15.Text.Trim) & ", " &
                IIf(txttc195.Text = "", "NULL", txttc195.Text.Trim) & ", " &
                IIf(txttc20.Text = "", "NULL", txttc20.Text.Trim) &
                ")"
        Else
            sql = "update ChamCongTay set" &
                " hc = " & IIf(txthc.Text = "", "NULL", txthc.Text.Trim) &
                ", tc15 = " & IIf(txttc15.Text = "", "NULL", txttc15.Text.Trim) &
                ", tc195 = " & IIf(txttc195.Text = "", "NULL", txttc195.Text.Trim) &
                ", tc20 = " & IIf(txttc20.Text = "", "NULL", txttc20.Text.Trim) &
                " where manv = N'" & cbomanv.SelectedValue.ToString() & "' and ngay = '" & String.Format("{0:yyyy-MM-dd}", dtngay.Value) & "'"
        End If
        ProcFuncBus.ExecuteProcSQL(sql)
        sql = "exec sp_laychamcongtaytheongay '" & String.Format("{0:yyyy-MM-dd}", dtngay.Value) & "'"
        dgvchamcongtay.DataSource = ProcFuncBus.ExecuteFuncSQL(sql)
        EnableText(True)
        dtngay.Enabled = False
        cbomanv.Enabled = False
        thaotac = "sua"
    End Sub

    Private Sub XóaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XóaToolStripMenuItem.Click
        Dim res As MsgBoxResult = MsgBox("Ban co muon xoa", MsgBoxStyle.YesNo, "Thong bao")
        Select res
            Case MsgBoxResult.Ok
                Dim sql As String = "delete from ChamCongTay where ngay = '" & String.Format("{0:yyyy-MM-dd}", dtngay.Value) & "' and manv = N'" & cbomanv.SelectedValue.ToString() & "'"
                ProcFuncBus.ExecuteProcSQL(sql)
            Case MsgBoxResult.No
                Return
        End Select

    End Sub

    Private Sub dgvchamcongtay_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvchamcongtay.CellClick
        Try
            GanText(e.RowIndex)
        Catch ex As Exception
            Return
        End Try

    End Sub
End Class